package top.luoblog.strategy;

import org.junit.Test;

/**
 * @author : luo
 * @description : 策略模式: 定义了算法族, 分别封装起来, 让他们直接可以互相替换, 此模式让算法的变化独立于算法的适用客户.
 */
public class StrategyTest {

    @Test
    public void test1() throws Exception {
        Calculator calculator = new Calculator(new OperationAdd());
        System.out.println(calculator.calculate(1, 2));

        calculator = new Calculator(new OperationSubtract());
        System.out.println(calculator.calculate(1, 2));

        calculator = new Calculator(new OperationMultiply());
        System.out.println(calculator.calculate(1, 2));

        calculator = new Calculator(new OperationDivide());
        System.out.println(calculator.calculate(1, 2));

        calculator = new Calculator(new Operation() {
            @Override
            public int doOperation(int num1, int num2) {
                return num1 + num2 * num2;
            }
        });
        System.out.println(calculator.calculate(1, 2));
    }

}
